/**
 * Advanced Software Engineering - assignment part 1 (monica Farrow)
 * ParcelUncolleted class
 * 
 * Some of the methods in this class have been based on lecture notes provided by
 * Monica Farrow as a part of Software Engineering courses at Heriot-Watt University.
 */

package depot;

import java.util.*;

/**
 * Advanced Software Engineering - F21AS.
 * ParcelUncolleted class
 * @author Ionut Iordache, Nikky Afunugo, Dave Bond
 */
public class ParcelUncollected {

	private Map<String, Parcel> parcelList = new HashMap<String, Parcel>();

	
	/** 
	 * constructor for ParcelUncolleted
	 * creates an empty HashMap for object type Parcel
	 */ 
	public ParcelUncollected() { 
		parcelList = new HashMap<String, Parcel>();
	} 

	
	/** 
	 * converts the HashMap into an ArrayList for production of the final report
	 * @return	the ArrayList<Parcel>
	 */ 
	public ArrayList<Parcel> hashMapToArray() {
		ArrayList<Parcel> list = new ArrayList<Parcel>(parcelList.values());
		return list;
	}

	
	/** 
	 * removes a Parcel from parcelList
	 * @return	Boolean indicating success or failure
	 */ 
	public Boolean removeParcel(String id) {
		Boolean isOK = true;
		if(parcelList.remove(id) != null) {
			return isOK;
		}
		isOK = false;
		return isOK;
	}

	
	/** 
	 * takes ID and  returns the Parcel object with matching ID
	 * @param id	the ID of the parcel
	 * @return the Parcel or null
	 */
	public Parcel getParcelByID(String id) {
		if (parcelList.containsKey(id)) {
			Parcel pcl = parcelList.get(id);
			return pcl;
		}
		return null;
	}
	
	/**
	 * Processes each line from processInputFIle, extracts data,
	 * creates Parcel object and adds to parcelList.
	 * Each line contains: ID, length, height, width, weight, days
	 * @param line the line to be processed
	 * @throws IllegalArgumentException, DuplicateIDException, ArrayIndexOutOfBoundsException, NumberFormatException  
	 */
	public void processLine(String line, int lineNum) throws IllegalArgumentException, DuplicateIDException, ArrayIndexOutOfBoundsException, NumberFormatException {

		String details [] = line.split(",");

		// take id
		String id = details[0];
		id = id.trim();

		// checks parcelList for duplicate ID
		Parcel pcl99 = getParcelByID(id);
		if(pcl99 != null) {
			throw new DuplicateIDException("ParcelID "+id+" already exists in uncollected parcels list");
		}

		// creates int [] and passes in dimensions, converted from String
		int dims[] = new int[3];
		for (int i=0; i < 3; i++) {
			dims[i] = Integer.parseInt(details[i+1]);
		} 

		// take weight
		double weight = Double.parseDouble(details[4]);

		// take days
		int days = Integer.parseInt(details[5]);

		// creates new Parcel object and adds to parcelList
		Parcel pcl = new Parcel(id, dims, weight, days);
		parcelList.put(id, pcl);
	}

}
